<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace form\element\form;

use form\element\form\traits\Options;
use form\element\traits\Classes;
use form\element\traits\Color;
use form\element\traits\Css;
use form\element\traits\Id;
use form\element\traits\Js;

class Ckeditor extends Column
{
    use Css, Js, Id, Classes, Color, Options;

    protected $value;

    public function value(string $value)
    {
        $this->value = $value;
        return $this;
    }

    protected function getOptions(): string
    {
        if (empty($this->options)) {
            $this->options = 'image_previewText: " ",
        filebrowserImageUploadUrl: "http://example.itshubao.com/index/test/upload/type/ckeditor"';
        }
        return is_array($this->options) ? json_encode($this->options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $this->options;
    }

    public function generate()
    {
        $label = $this->getLabel();
        $labelId = $this->getLabelFor();
        $labelClass = $this->getLabelClass();
        $name = $this->name;
        $class = $this->getClass();
        $id = $this->getId();
        $value = $this->value;
        $this->loadJs = [
            'http://libs.itshubao.com//ckeditor/ckeditor.js',
        ];

        $this->js = <<<HTML

$(document).ready(function(){
    CKEDITOR.replace('$labelId', {
        image_previewText: ' ',
        filebrowserImageUploadUrl: "http://example.itshubao.com/index/test/upload/type/ckeditor"
    });
});
HTML;

        return <<<HTML

<div class="$class">
<label for="$labelId" class="$labelClass">$label</label>
<textarea id="$id" name="$name">$value</textarea>
</div>
HTML;

    }

    /**
     * @inheritDoc
     */
    protected function normal()
    {
        // TODO: Implement normal() method.
    }

    /**
     * @inheritDoc
     */
    protected function vertical()
    {
        // TODO: Implement vertical() method.
    }

    /**
     * @inheritDoc
     */
    protected function inline()
    {
        // TODO: Implement inline() method.
    }
}